package com.nf.controller.manage;

import com.google.gson.Gson;
import com.nf.controller.BaseServlet;
import com.nf.dao.FilmInfoDao;
import com.nf.dao.impl.FilmInfoDaoImpl;
import com.nf.entity.FilmInfo;
import com.nf.util.PagerUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * 编写servlet，分页查询影片
 */
@WebServlet("/api/FilmListByPagerServlet.do")
public class FilmListByPagerServlet extends BaseServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //从客户端中，获取当前页参数
        String cp=req.getParameter("cp");

        //设置默认当前页为第一页
        int currentPage=1;
        if(cp!=null){
            currentPage= Integer.parseInt(cp);
        }

        FilmInfoDao dao=new FilmInfoDaoImpl();

        // currentPage:当前页
        //设置每页显示记录数为2
        PagerUtil<FilmInfo> pager= dao.selectAllByPager(currentPage,2);

        resp.setContentType("application/json;charset=utf-8");
        PrintWriter out = resp.getWriter();

        out.print(new Gson().toJson(successJson(pager)));
        out.flush();
        out.close();
    }
}
